/**
 * 描述：生命周期实例管理
 * 作者：彭涛涛
 * 日期：2019年6月24日
 * 版本：0.0.1
 **/
(function(E, KIT, RES) {
    "use strict";

    var __URL_ROOT = RES.URL_ROOT + "/lifecycexample";
    var __example_attrib01;

    //region private  生命周期实例新增编辑操作
    var _addOrUpdate_example = function(e) {

        var __grid = this;
        rapid.confirm("确定要保存吗？", function(yes) {
            if(yes) {
                var __attrib_02 = __example_attrib01;

                rapid.post({
                    url: __URL_ROOT + "/saveLifecycExample",
                    data: $.extend(true, {}, e.serializeJson(), {attrib_02: __attrib_02}),
                    success: function(result) {
                        e.hide();

                        rapid.success("保存成功。", function() {
                            __grid.reload();
                            __example_attrib01 = "";
                        }, false);
                    }
                });
            }
        });
    };
    //endregion

    //region private  生命周期实例节点编辑操作
    var _addOrUpdate_example_node = function(e) {
        var __slt = rapid("#grdLifecycExample").grid().selection()[0];
        if(rapid.isEmpty(__slt)) {
            rapid.warn("请选中一条生命周期实例");
            return;
        }

        var __grid = this;
        rapid.confirm("确定要保存吗？", function(yes) {
            if(yes) {
                rapid.post({
                    url: __URL_ROOT + "/saveLifecycExampleNode",
                    data: e.serializeJson(),
                    success: function(result) {
                        e.hide();

                        rapid.success("保存成功。", function() {
                            __grid.reload();
                        }, false);
                    }
                });
            }
        });
    };
    //endregion

    //region private  生命周期实例参与人员编辑操作
    var _addOrUpdate_example_node_per = function(e) {
        var __row_id = rapid("#grdExampleNode").grid().selection()[0].ui().conflict_id.val();
        var __grid = this;
        rapid.confirm("确定要保存吗？", function(yes) {
            if(yes) {
                rapid.post({
                    url: __URL_ROOT + "/saveLifecycExamplePER",
                    data: $.extend(true, {}, e.serializeJson(), {row_id: __row_id}),
                    success: function(result) {
                        e.hide();

                        rapid.success("保存成功。", function() {
                            __grid.reload();

                        }, false);
                    }
                });
            }
        });
    };
    //endregion

    //富文本框初始化
    var _getGridFields_ = [{
            title: "标题", name: "attrib_01", frozen: true, width: 100,
            editor: {
                validator: {
                    rules: {required: true, maxlength: 100}
                }
            }
        }, {
            title: '生命周期模型定义', name: 'attrib_02', colspan: 2, visible: false,
            editor: {
                name: "row_id",
                type: "picker",
                textField: "attrib_02",
                tip: "请选择...",
                autoReload: true,
                editable: true,
                dataSource: {
                    textField: "attrib_01",
                    valueField: "conflict_id",
                    url: __URL_ROOT + "/getLifecycModel"
                },
                autocomplete: {
                    paramName: "search_name",
                    dataSource: {
                        textField: "attrib_01",
                        valueField: "conflict_id",
                        url: __URL_ROOT + "/getLifecycModel"
                    }
                },
                validator: {
                    rules: {
                        required: true
                    }
                },
                onchange: function() {
                    __example_attrib01 = this.text();
                }
            }
        }, {
            title: '编号', name: 'attrib_03', width: 100,
            editor: {
                validator: {
                    rules: {required: true, maxlength: 100}
                }
            }
        }, {
            title: "有效截止日期", name: "attrib_14", width: 100,
            formatter: rapid.date.removeTime,
            editor: {
                type: "datetimepicker", mode: "yMd", textFormat: "yyyy年MM月dd日",
                validator: {
                    rules: {required: true}
                }
            }
        }, {
            title: "菜单目录", name: "menu_type", visible: false,
            editor: {
                tip: "请选择...",
                name: "pid",
                type: "picker",
                editable: false,
                dataSource: {
                    textField: "name",
                    valueField: "id",
                    url: __URL_ROOT + "/getExampleMenus"
                }
            }
        }, {
            title: "企业名称", name: "enterprisename", width: 160,
            editor: {
                name: "attrib_05",
                type: "picker",
                tip: "请选择...",
                textField: "enterprise_name",
                valueField: "attrib_05",
                editable: true,
                dataSource: {
                    textField: "enterprise_name",
                    valueField: "conflict_id",
                    url: __URL_ROOT + "/getEnterpriseName"
                },
                autocomplete: {
                    paramName: "search_name",
                    dataSource: {
                        textField: "enterprise_name",
                        valueField: "conflict_id",
                        url: __URL_ROOT + "/getEnterpriseName"
                    }
                },
                validator: {
                    rules: {
                        required: true
                    }
                }
            }
        },/* {
            title: "会议类型", name: "meet_type",
            editor: {
                tip: "请选择...",
                name: "attrib_04",
                textField: "meet_type",
                type: "picker",
                editable: false,
                dataSource: {
                    textField: "codename",
                    valueField: "customid",
                    url: __URL_ROOT + "/getMeetType"
                }
            }
        },*/ {
            title: "是否结束", name: "attrib_16_cn", visible: true,
            editor: {
                name:"attrib_16",
                type: "checkbox",
                disabled: false,
                checked: false,
                mode: "switch",
                text: ["已结束", "未结束"],
                value: [1, 0]
            }
        }, {
            title: "状态", name: "attrib_15", visible: false,
            editor: {
                type: "checkbox",
                disabled: false,
                checked: true,
                mode: "switch",
                text: ["启用", "禁用"],
                value: [1, 0]
            }
        },{ title: "人员", name: "attrib_21_cn", width: 150,visible: false, editor: {
                name: "attrib_21",
                type: "picker",
                tip: "请选择...",
                textField: "attrib_21_cn",
                valueField: "attrib_21",
                multiple: true,
                autoReload: true,
                editable: true,
                dataSource: {
                    textField: "name",
                    valueField: "id",
                    url: __URL_ROOT + "/getUsers"
                },
                autocomplete: {
                    paramName: "search_name",
                    dataSource: {
                        textField: "name",
                        valueField: "id",
                        url: __URL_ROOT + "/getUsers"
                    }
                }
            }
        }, {
            title: '描述', name: 'attrib_19', size: 12, visible: false,
            editor: KIT.tmplWangEditor()
        }],
        _getGridSingeActions_ = [{
            "创建菜单": function(e) {
                _createExampleMenu(rapid("#grdLifecycExample").grid().selection()[0]);
            }
        }],
        _initExampleMenu_ = function() {
            rapid("#frmExampleMenu").dataview({
                colSize: 1,
                fields: [{
                    title: "菜单", name: "menu_type", width: 220, editor: {
                        name: "pid",
                        type: "picker",
                        tip: "请选择...",
                        editable: false,
                        dataSource: {
                            textField: "name",
                            valueField: "id",
                            url: __URL_ROOT + "/getExampleMenus"
                        },
                        validator: {
                            rules: {required: true}
                        }
                    }
                }]
            });
        },
        _createExampleMenu = function(__slt) {
            var __grid = this;
            rapid("#wndExampleMenu").window({
                title: "创建菜单",
                size: "xs",
                buttons: [{
                    "保存": function(e) {
                        var s = this;
                        var __dataview = rapid("#frmExampleMenu").dataview();

                        if(!__dataview.valid()) {
                            return;
                        }
                        rapid.confirm("确定要保存吗？", function(e) {
                            var data = __dataview.serializeJson();
                            data.exampleName = __slt.ui("attrib_01").val();
                            data.exampleCode = __slt.ui("attrib_03").val();
                            if(e) {
                                rapid.post({
                                    id: "saveExampleMenu",
                                    url: __URL_ROOT + "/saveExampleMenu",
                                    data: data,
                                    success: function(result) {
                                        s.hide();
                                        __grid.reload();
                                    }
                                });
                            }
                        });
                    }
                }],
                onHide: function() {
                    rapid("#frmExampleMenu").dataview().reset();
                }
            }).show();
        },
        // 生命周期实例
        _initGrid_ = function() {
            var grid = rapid("#grdLifecycExample").grid({
                height: "100%",

                addable: true,
                updatable: true,
                removable: false,
                detailed: true,

                rowHeight: "always",

                dataSource: {
                    url: __URL_ROOT + "/getObjects"
                },
                pageable: true,
                keyFields: "conflict_id",
                paramFields: ["conflict_id"],
                fields: _getGridFields_,
                singleActions: _getGridSingeActions_
            });

            grid.on("loadsuccess", function() {
                rapid("#grdExampleNode").grid().clear();
                rapid("#grdExampleNodeACC").grid().clear();
                rapid("#grdExampleNodePER").grid().clear();
            })
                .on("click", function(e) {
                    var __conflict_id = this.current().ui().conflict_id.val();
                    rapid("#grdExampleNode").grid().load({
                        data: "row_id=" + __conflict_id
                    });
                })
                .on("add", _addOrUpdate_example)
                .on("update", _addOrUpdate_example)
                .on("beforeUpdate", function(dataview) {
                    dataview.ui("row_id").disabled(true);
                });

        },
        _initUi_ = function() {
            /* var _app = rapid("#grdLifecycExample").grid();*/
            //查询
            rapid.ui("#txtSearch").on("submit", function() {
                rapid("#grdLifecycExample").grid().load({
                    data: {
                        attrib_01: this.val()
                    }
                });
            });
        },
        //endregion

        //region生命周期实例节点
        _getGridNodeFields_ = [{
            title: "节点名称", name: "attrib_01", width: 200
        }, {
            title: "顺序编号", name: "attrib_11", width: 100
        }, {
            title: "表单名称", name: "attrib_13", width: 200
        }, {
            title: "有效截止日期", name: "attrib_14", width: 150,
            formatter: rapid.date.removeTime,
            editor: {
                type: "datetimepicker", mode: "yMd", textFormat: "yyyy年MM月dd日",
                validator: {
                    rules: {required: true}
                }
            }
        },{
            title: '关联标签', name: 'attrib_21', visible: true,

            editor: {
                name: "attrib_21",
                type: "picker",
                tip: "请选择...",
                autoReload: true,
                editable: true,
                multiple: true,
                dataSource: {
                    textField: "name",
                    valueField: "name",
                    url: __URL_ROOT + "/getTabName"
                },
                autocomplete: {
                    paramName: "search_name",
                    dataSource: {
                        textField: "name",
                        valueField: "name",
                        url: __URL_ROOT + "/getTabName"
                    }
                }/*,
            validator: {
                rules: {
                    required: true
                }
            }*/
            }
        }, {
            title: '描述', name: 'attrib_20', size: 12, visible: false,
            editor: KIT.tmplWangEditor()
        }],
        _initGridNode_ = function() {
            rapid("#grdExampleNode")
                .grid({
                    height: "100%",
                    addable: false,
                    updatable: true,
                    removable: false,
                    detailed: true,

                    dataSource: {
                        url: __URL_ROOT + "/getExampleNode"
                    },
                    pageable: true,
                    keyFields: "conflict_id",
                    paramFields: ["row_id", "conflict_id"],
                    fields: _getGridNodeFields_

                })
                .on("loadsuccess", function() {
                    rapid("#grdExampleNodeACC").grid().clear();
                    rapid("#grdExampleNodePER").grid().clear();
                })
                .on("click", function(e) {
                    var __conflict_id = this.current().ui().conflict_id.val();
                    rapid("#grdExampleNodeACC").grid().load({
                        data: "row_id=" + __conflict_id
                    });
                    rapid("#grdExampleNodePER").grid().load({
                        data: "row_id=" + __conflict_id
                    });
                })
                .on("add", _addOrUpdate_example_node)
                .on("beforeUpdate", function(e) {

                    var __value = rapid("#grdLifecycExample").grid().selection()[0].ui().attrib_14.val();
                    e.ui("attrib_14").max(__value);
                })
                .on("update", _addOrUpdate_example_node);

        },
        //endregion

        //region生命周期节点附件
        _getGridNodeACCFields_ = [{
            title: "附件类型", name: "attrib_01_cn", width: 200
        }, {
            title: "是否必填", name: "attrib_15_cn", width: 100
        }],
        _initGridNodeACC_ = function() {
            rapid("#grdExampleNodeACC").grid({
                height: "100%",
                addable: false,
                updatable: false,
                removable: false,
                detailed: true,

                dataSource: {
                    url: __URL_ROOT + "/getExampleNodeACC"
                },
                pageable: true,
                keyFields: "conflict_id",
                paramFields: ["row_id"],
                dataview: {
                    colSize: 2
                },
                window: {
                    size: "xl"
                },
                fields: _getGridNodeACCFields_
            });

        },
        //endregion

        //region生命周期节点参与人员
        _getGridNodePERFields_ = [{
            title: "人员", name: "attrib_01_cn", width: 150, editor: {
                name: "attrib_01",
                type: "picker",
                tip: "请选择...",
                textField: "attrib_01_cn",
                multiple: true,
                autoReload: true,
                editable: true,
                dataSource: {
                    textField: "name",
                    valueField: "id",
                    url: __URL_ROOT + "/getUsers"
                },
                autocomplete: {
                    paramName: "search_name",
                    dataSource: {
                        textField: "name",
                        valueField: "id",
                        url: __URL_ROOT + "/getUsers"
                    }
                },
                validator: {
                    rules: {
                        required: true
                    }
                }
            }
        }, {
            title: "类型", name: "attrib_02_cn", width: 150,
            editor: {
                name: "attrib_02",
                type: "picker",
                tip: "请选择...",
                textField: "attrib_02_cn",
                dataSource: {
                    textField: "codename",
                    valueField: "customid",
                    url: __URL_ROOT + "/getUserType"
                },
                validator: {
                    rules: {
                        required: true
                    }
                }
            }

        }],
        _initGridNodePER_ = function() {
            rapid("#grdExampleNodePER")
                .grid({
                    height: "100%",
                    addable: true,
                    updatable: true,
                    removable: true,
                    detailed: true,

                    editMode: "popup", // popup、inner

                    dataSource: {
                        url: __URL_ROOT + "/getExampleNodePER"
                    },
                    pageable: true,
                    keyFields: "conflict_id",
                    paramFields: ["conflict_id", "row_id"],
                    dataview: {
                        colSize: 1
                    },
                    window: {
                        size: "sm"
                    },
                    fields: _getGridNodePERFields_
                })
                .on("add", _addOrUpdate_example_node_per)
                .on("beforeAdd", function() {
                    var __slt = rapid("#grdExampleNode").grid().selection()[0];
                    if(rapid.isEmpty(__slt)) {
                        rapid.warn("请选中一条生命周期实例节点");
                        return false;
                    }
                })
                .on("update", _addOrUpdate_example_node_per)
                .on("beforeUpdate", function(dataview) {
                    dataview.ui("attrib_01").disable(true);
                })
                .on("remove", function(ids) {
                    var __grid = this;
                    rapid.post({
                        url: __URL_ROOT + "/deleteExamplePER",
                        data: "ids=" + ids,
                        success: function() {
                            __grid.reload();
                        }
                    });
                });

        };
    //endregion

    rapid.LifecycExample(function() {

        _initUi_();

        _initGrid_();

        _initGridNode_();

        _initGridNodeACC_();

        _initGridNodePER_();

        _initExampleMenu_();

        rapid("#grdLifecycExample").grid().load();

    }).init();

})(rapid.html.Element, rapid.kit, rapid.res);